﻿using MvcApplication2.Models.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MvcApplication2.Models.Services
{
    public class NguoiDungSanPhamService
    {
        public static NguoiDungSanPham GetById(int idNguoiDungSanPham)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@ID", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDungSanPham;
            var nguoiDungSanPham =
                Utility.ToList<NguoiDungSanPham>(
                    MyDataProvider.excuteQueryWithParameters("spu_nguoidung_sanpham_getById", sqlParameterList))[0];
            MyDataProvider.Close();
            return nguoiDungSanPham;
        }

        public static string DatMuaSanPham(NguoiDungSanPham nguoiDungSanPham)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDung", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdNguoiDung;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdSanPham;
            sqlParameterList.Add(new SqlParameter("@SoLuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.SoLuong;
            sqlParameterList.Add(new SqlParameter("@DonGia", SqlDbType.Decimal));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.DonGia;
            sqlParameterList.Add(new SqlParameter("@TongGia", SqlDbType.Decimal));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.TongGia;
            sqlParameterList.Add(new SqlParameter("@IDTrangThaiSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdTrangThaiSanPham;
            sqlParameterList.Add(new SqlParameter("@IDDaiLyNhanSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdDaiLyNhanSanPham;
            sqlParameterList.Add(new SqlParameter("@DiemThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.DiemThuong;
            sqlParameterList.Add(new SqlParameter("@TongDiemThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.TongDiemThuong;
            try
            {
                MyDataProvider.insertQueryWithParameters("spu_datmuasanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Đặt mua thành công";
        }

        public static string DoiThuongSanPham(NguoiDungSanPham nguoiDungSanPham)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDung", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdNguoiDung;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdSanPham;
            sqlParameterList.Add(new SqlParameter("@SoLuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.SoLuong;
            sqlParameterList.Add(new SqlParameter("@DonGia", SqlDbType.Decimal));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.DonGia;
            sqlParameterList.Add(new SqlParameter("@TongGia", SqlDbType.Decimal));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.TongGia;
            sqlParameterList.Add(new SqlParameter("@IDTrangThaiSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdTrangThaiSanPham;
            sqlParameterList.Add(new SqlParameter("@IDDaiLyNhanSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.IdDaiLyNhanSanPham;
            sqlParameterList.Add(new SqlParameter("@DiemThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.DiemThuong;
            sqlParameterList.Add(new SqlParameter("@TongDiemThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = nguoiDungSanPham.TongDiemThuong;
            try
            {
                MyDataProvider.insertQueryWithParameters("spu_nhanthuongsanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Đổi thưởng thành công";
        }

        public static List<NguoiDungSanPham> LayDanhSachByIdTaiKhoan(int idTaiKhoan,int isPhanThuong)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IdNguoiDung", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idTaiKhoan;
            sqlParameterList.Add(new SqlParameter("@IsPhanThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = isPhanThuong;
            var nguoiDungSanPhamList = Utility.ToList<NguoiDungSanPham>(MyDataProvider.excuteQueryWithParameters(
                "spu_nguoidung_sanpham_getByIdNguoiDung", sqlParameterList));
            MyDataProvider.Close();
            return nguoiDungSanPhamList;
        }

        public static string HuyDatMua(int idNguoiDungSanPham,int idSanPham,int soLuong)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDungSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDungSanPham;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idSanPham;
            sqlParameterList.Add(new SqlParameter("@SoLuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = soLuong;
            try
            {
                MyDataProvider.deleteQueryWithParameters("spu_huymuasanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Huỷ đặt mua sản phẩm thành công";
        }

        public static string CapNhatDatMuaSanPham(int idNguoiDungSanPham,int idSanPham,int soLuong,int idDaiLy)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDungSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDungSanPham;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idSanPham;
            sqlParameterList.Add(new SqlParameter("@SoLuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = soLuong;
            sqlParameterList.Add(new SqlParameter("@IDDaiLy", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idDaiLy;
            try
            {
                MyDataProvider.updateQueryWithParameters("spu_capnhatdatmuasanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Cập nhật thành công";
        }

        public static string HuyNhanThuong(int idNguoiDungSanPham, int idSanPham, int soLuong, int idNguoiDung, int tongDiemThuong)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDungSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDungSanPham;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idSanPham;
            sqlParameterList.Add(new SqlParameter("@SoLuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = soLuong;
            sqlParameterList.Add(new SqlParameter("@IDNguoiDung", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDung;
            sqlParameterList.Add(new SqlParameter("@TongDiemThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = tongDiemThuong;
            try
            {
                MyDataProvider.deleteQueryWithParameters("spu_huynhanthuongsanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Huỷ nhận thưởng sản phẩm thành công";
        }

        public static List<NguoiDungSanPham> LayDanhSachXacNhanHang(string username,int idTrangThaiSanPham,int isPhanThuong,int idDaiLy)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@Username", username));
            sqlParameterList.Add(new SqlParameter("@IDTrangThaiSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idTrangThaiSanPham;
            sqlParameterList.Add(new SqlParameter("@IsPhanThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = isPhanThuong;
            sqlParameterList.Add(new SqlParameter("@IDDaiLy", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idDaiLy;
            var nguoiDungSanPhamList = Utility.ToList<NguoiDungSanPham>(MyDataProvider.excuteQueryWithParameters("spu_nguoidung_sanpham_getByUsernameIdTrangThaiSanPham", sqlParameterList));
            MyDataProvider.Close();
            return nguoiDungSanPhamList;
        }

        public static string XacNhanHang(int idNguoiDungSanPham,int idSanPham,int idNguoiDung,int isPhanThuong)
        {
            MyDataProvider.Open();
            var sqlParameterList = new List<SqlParameter>();
            sqlParameterList.Add(new SqlParameter("@IDNguoiDungSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDungSanPham;
            sqlParameterList.Add(new SqlParameter("@IDSanPham", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idSanPham;
            sqlParameterList.Add(new SqlParameter("@IDNguoiDung", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = idNguoiDung;
            sqlParameterList.Add(new SqlParameter("@IsPhanThuong", SqlDbType.Int));
            sqlParameterList[sqlParameterList.Count - 1].Value = isPhanThuong;
            try
            {
                MyDataProvider.updateQueryWithParameters("spu_xacnhansanpham", sqlParameterList);
            }
            catch (SqlException exception)
            {
                MyDataProvider.Close();
                return exception.Message;
            }
            MyDataProvider.Close();
            return "Xác nhận hàng thành công";
        }
    }
}